package com.shenmazong.servereduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servereduuser.entity.TbPermission;
import com.shenmazong.servereduuser.mapper.TbPermissionMapper;
import com.shenmazong.servereduuser.service.TbPermissionService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 *
 */
@Service
public class TbPermissionServiceImpl implements TbPermissionService {
    @Autowired
    private TbPermissionMapper permissionMapper;

    @Override
    public ResultResponse getOnePermission(TbPermission permission) {
        if(permission.getPermissionId()==null || permission.getPermissionId()==0){
            return ResultResponse.FAILED(404,"权限id 不能为空");
        }
        TbPermission tbPermission = permissionMapper.selectOne(
                new QueryWrapper<TbPermission>().lambda().eq(TbPermission::getPermissionId, permission.getPermissionId())
        );
        if(tbPermission==null){
            return ResultResponse.FAILED(500,"无此权限记录");
        }

        return  ResultResponse.SUCCESS(tbPermission);
    }

    @Override
    public ResultResponse addPermission(TbPermission tbPermission) {
        try {
            permissionMapper.insert(tbPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(401,"添加权限记录失败");
        }
        return ResultResponse.SUCCESS(200,"添加权限记录成功");
    }

    @Override
    public ResultResponse deletePermission(BatchChooseIdVo chooseIdVo) {
        if(chooseIdVo.getIds().size()<=0){
            return ResultResponse.FAILED(401,"请至少选择一个权限");
        }

        try {
            permissionMapper.deleteBatchIds(chooseIdVo.getIds());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"删除权限记录失败");
        }
        return ResultResponse.SUCCESS(200,"删除权限记录成功");
    }

    @Override
    public ResultResponse updatePermission(TbPermission permission) {
        if(permission.getPermissionId()==null || permission.getPermissionId()==0){
            return ResultResponse.FAILED(404,"权限id 不能为空");
        }

        try {
            permissionMapper.updateById(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"更新权限记录失败");
        }

        return ResultResponse.SUCCESS(200,"更新权限记录成功");
    }
}




